package com.yz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yz.common.DishException;
import com.yz.dto.DishDto;
import com.yz.entity.Category;
import com.yz.entity.Dish;
import com.yz.entity.DishFlavor;
import com.yz.mapper.DishMapper;
import com.yz.service.CategoryService;
import com.yz.service.DishFlavorService;
import com.yz.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 获取菜品分页数据 并添加菜品分类名称
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<DishDto> pageAddCategoryName(int page,int pageSize,String name) {
        //获取Dish分页数据
        Page<Dish> dishPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),Dish::getName,name);
        wrapper.orderByAsc(Dish::getSort);
        wrapper.orderByDesc(Dish::getUpdateTime);
        this.page(dishPage,wrapper);
        //添加菜品分类名称
        Page<DishDto> dishDtoPage = new Page<>();

        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");
        List<DishDto> collect = dishPage.getRecords().stream().map(item -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item,dishDto);
            Category category = categoryService.getById(item.getCategoryId());
            if (category == null)
                return dishDto;
            dishDto.setCategoryName(category.getName());
            return dishDto;
        }).collect(Collectors.toList());
        //设置分页数据
        dishDtoPage.setRecords(collect);
        return dishDtoPage;
    }

    /**
     * 根据id 获取dish  并添加菜品分类名称
     * @param id
     * @return
     */
    @Override
    public DishDto getDishDtoById(Long id) {
        //根据id获取dish数据
        Dish dish = this.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //根据dish 获取菜品分类名称
        Category category = categoryService.getById(dish.getCategoryId());
        dishDto.setCategoryName(category.getName());

        //根据dish_id  获取dish_flavor表中的口味数据 添加到list集合中
        LambdaQueryWrapper<DishFlavor> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> list = dishFlavorService.list(wrapper);
        dishDto.setFlavors(list);
        return dishDto;
    }
    /**
     * 根据categoryId(菜品分类id)  获取响应菜品分类的菜品列表
     * @param categoryId
     * @return
     */
    @Override
    public List<DishDto> listAndFlavors(Long categoryId) {
        //查询条件  根据CategoryId查询  且status状态为1（启售状态）  并根据更加时间降序排序
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId,categoryId);
        wrapper.eq(Dish::getStatus,1);
        wrapper.orderByDesc(Dish::getUpdateTime);
        List<Dish> list = this.list(wrapper);
        // 查询每个Dish的dish_flavor(口味数据)
        List<DishDto> collect = list.stream().map(item -> {
            LambdaQueryWrapper<DishFlavor> dishFlavorWrapper = new LambdaQueryWrapper<>();
            DishDto dishDto = new DishDto();
            // 将Dish中的数据 复制到 DishDto中
            BeanUtils.copyProperties(item, dishDto);
            // 根据DishId查询 dish_flavor菜品口味表中对应的口味数据
            dishFlavorWrapper.eq(DishFlavor::getDishId, item.getId());
            List<DishFlavor> list1 = dishFlavorService.list(dishFlavorWrapper);
            dishDto.setFlavors(list1);
            return dishDto;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 删除菜品  根据id  可多id同时删除
     * @param ids
     * @return
     */
    @Transactional
    @Override
    public Boolean deleteByids(Long...ids) {
        List<Long> idList = Arrays.asList(ids);
        int count = 0;
        // 遍历每个id
        for(Long id:ids){
            // 根据id 删除dish_flavor（口味表） 表中对应的口味数据
            LambdaQueryWrapper<DishFlavor> wrapperDishFlavor = new LambdaQueryWrapper<>();
            wrapperDishFlavor.eq(DishFlavor::getDishId, id);
            boolean remove = dishFlavorService.remove(wrapperDishFlavor);
            // 根据id 删除dish表中的数据
            this.removeById(id);
            count += remove?1:0;
        }
        // 判断传入的ids是否被全部删除  如果没有则返回 "没有完全删除" 同时业务回滚
        if(count != ids.length)
            throw new DishException("没有完全删除");
        return true;
    }

}
